package com.tongzhan.controller;

import com.tongzhan.base.Return;
import com.tongzhan.entity.User;
import com.tongzhan.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Value("${head.url}")
    private String headUrl;

    @Resource
    private UserService userService;

    /**
     * 新增用户接口
     *
     * @param userName     用户名
     * @param realName     真实姓名
     * @param duties       职务
     * @param dutiesCode   权限编码
     * @param streetCode   街道编码
     * @param villageCode  村社区编码
     * @param siteCode     宗教场所编码
     * @param religionCode 宗教团体编码
     * @return Return
     */
    @PostMapping("/addUser")
    public Return addUser(String userName, String realName, String duties, String dutiesCode, String streetCode,
                          String villageCode, String siteCode, String religionCode) {
        if (StringUtils.isEmpty(userName) || "".equals(userName.trim())
                || StringUtils.isEmpty(realName) || "".equals(realName.trim())
                || StringUtils.isEmpty(duties) || "".equals(duties.trim()) || StringUtils.isEmpty(dutiesCode)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        if (userName.trim().length() > 50) {
            return new Return(Return.FAIL, "用户名过长");
        }
        if (realName.trim().length() > 30) {
            return new Return(Return.FAIL, "真实姓名过长");
        }
        if (duties.trim().length() > 100) {
            return new Return(Return.FAIL, "职务过长");
        }
        User user = new User();
        switch (dutiesCode) {
            case "02":
                //街道
                if (StringUtils.isEmpty(streetCode)) {
                    return new Return(Return.FAIL, "请选择街道信息");
                }
                user.setStreetCode(streetCode);
                break;
            case "03":
                //宗教团体
                if (StringUtils.isEmpty(religionCode)) {
                    return new Return(Return.FAIL, "请选择宗教信息");
                }
                user.setReligionCode(religionCode);
                break;
            case "04":
            case "07":
                //宗教场所、民间信仰点
                if (StringUtils.isEmpty(streetCode)) {
                    return new Return(Return.FAIL, "请选择街道信息");
                }
                if (StringUtils.isEmpty(villageCode)) {
                    return new Return(Return.FAIL, "请选择村庄信息");
                }
                if (StringUtils.isEmpty(siteCode)) {
                    return new Return(Return.FAIL, "请选择场所信息");
                }
                //查询该场所是否存在账号
                User siteUser = userService.selectUserBySiteCode(siteCode);
                if (siteUser != null) {
                    return new Return(Return.FAIL, "该场所已存在账号，无法继续创建");
                }
                user.setStreetCode(streetCode);
                user.setVillageCode(villageCode);
                user.setSiteCode(siteCode);
                break;
            case "05":
                //村社区
                if (StringUtils.isEmpty(streetCode)) {
                    return new Return(Return.FAIL, "请选择街道信息");
                }
                if (StringUtils.isEmpty(villageCode)) {
                    return new Return(Return.FAIL, "请选择村庄信息");
                }
                user.setStreetCode(streetCode);
                user.setVillageCode(villageCode);
                break;
        }
        user.setUserName(userName.trim());
        user.setRealName(realName.trim());
        user.setDuties(duties.trim());
        user.setDutiesCode(dutiesCode);
        //用户默认头像
        user.setPicture(headUrl);
        return userService.addUser(user);
    }

    /**
     * 编辑用户接口(信息管理修改用户信息)
     *
     * @param userId       用户id
     * @param userName     用户名
     * @param realName     真实姓名
     * @param duties       职务
     * @param dutiesCode   权限编码
     * @param streetCode   街道编码
     * @param villageCode  村社区编码
     * @param siteCode     宗教场所编码
     * @param religionCode 宗教团体编码
     * @return Return
     */
    @PostMapping("/editUser")
    public Return editUser(String userId, String userName, String realName, String duties, String dutiesCode, String streetCode,
                           String villageCode, String siteCode, String religionCode) {
        if (StringUtils.isEmpty(userId) || "".equals(userId.trim())
                || StringUtils.isEmpty(userName) || "".equals(userName.trim())
                || StringUtils.isEmpty(realName) || "".equals(realName.trim())
                || StringUtils.isEmpty(duties) || "".equals(duties.trim()) || StringUtils.isEmpty(dutiesCode)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        if (userName.trim().length() > 50) {
            return new Return(Return.FAIL, "用户名过长");
        }
        if (realName.trim().length() > 30) {
            return new Return(Return.FAIL, "真实姓名过长");
        }
        if (duties.trim().length() > 100) {
            return new Return(Return.FAIL, "职务过长");
        }
        User user = new User();
        switch (dutiesCode) {
            case "02":
                //街道
                if (StringUtils.isEmpty(streetCode)) {
                    return new Return(Return.FAIL, "请选择街道信息");
                }
                user.setStreetCode(streetCode);
                break;
            case "03":
                //宗教团体
                if (StringUtils.isEmpty(religionCode)) {
                    return new Return(Return.FAIL, "请选择宗教信息");
                }
                user.setReligionCode(religionCode);
                break;
            case "04":
            case "07":
                //宗教场所、民间信仰点
                if (StringUtils.isEmpty(streetCode)) {
                    return new Return(Return.FAIL, "请选择街道信息");
                }
                if (StringUtils.isEmpty(villageCode)) {
                    return new Return(Return.FAIL, "请选择村庄信息");
                }
                if (StringUtils.isEmpty(siteCode)) {
                    return new Return(Return.FAIL, "请选择场所信息");
                }
                //查询该场所是否存在账号
                User siteUser = userService.selectUserBySiteCode(siteCode);
                if (siteUser != null) {
                    return new Return(Return.FAIL, "该场所已存在账号，无法继续创建");
                }
                user.setStreetCode(streetCode);
                user.setVillageCode(villageCode);
                user.setSiteCode(siteCode);
                break;
            case "05":
                //村社区
                if (StringUtils.isEmpty(streetCode)) {
                    return new Return(Return.FAIL, "请选择街道信息");
                }
                if (StringUtils.isEmpty(villageCode)) {
                    return new Return(Return.FAIL, "请选择村庄信息");
                }
                user.setStreetCode(streetCode);
                user.setVillageCode(villageCode);
                break;
        }
        user.setUserId(userId);
        user.setUserName(userName.trim());
        user.setRealName(realName.trim());
        user.setDuties(duties.trim());
        user.setDutiesCode(dutiesCode);
        //用户默认头像
        user.setPicture(headUrl);
        return userService.editUser(user);
    }

    /**
     * 用户修改密码接口
     *
     * @param userId      用户id
     * @param newPassword 新密码
     * @return Return
     */
    @PostMapping("/changePassword")
    public Return changePassword(String userId, String oldPassword, String newPassword) {
        if (StringUtils.isEmpty(newPassword) || "".equals(newPassword.trim())
                || StringUtils.isEmpty(oldPassword) || "".equals(oldPassword)
                || StringUtils.isEmpty(userId)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }

        return userService.changePassword(userId, oldPassword, newPassword);
    }

    /**
     * 编辑用户个人信息接口
     *
     * @param userId    用户id
     * @param realName  真实姓名
     * @param telephone 手机号码
     * @return Return
     */
    @PostMapping("/editUserInfo")
    public Return editUserInfo(String userId, String realName, String telephone) {
        if (StringUtils.isEmpty(realName) || "".equals(realName.trim())
                || StringUtils.isEmpty(telephone) || "".equals(telephone.trim())
                || StringUtils.isEmpty(userId)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        return userService.editUserInfo(userId, realName, telephone);
    }

    /**
     * 获取用户列表接口
     *
     * @param dutiesCode 权限编码
     * @param realName   真实姓名
     * @param page       页
     * @param size       大小
     * @return Return
     */
    @PostMapping("/getUserList")
    public Return getUserList(String dutiesCode, String realName, Integer page,
                              @RequestParam(required = false, defaultValue = "10") Integer size) {
        if (page == null) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        if (page <= 0) {
            page = 1;
        }
        return userService.getUserList(dutiesCode, realName, page, size);
    }

    /**
     * 用户登录接口
     *
     * @param username 用户名
     * @param password 密码
     * @return Return
     */
    @PostMapping("/login")
    public Return login(HttpServletResponse response, String username, String password) {
        if (StringUtils.isEmpty(username) || "".equals(username.trim()) || StringUtils.isEmpty(password) || "".equals(password.trim())) {
            return new Return(Return.MISS_PARAM, "用户名和密码不能为空");
        }
        return userService.login(response, username.trim(), password);
    }

    /**
     * 获取用户对应的模块标识接口
     *
     * @param userId 用户id
     * @return Return
     */
    @PostMapping("/getUserSign")
    public Return getUserSign(String userId) {
        if (StringUtils.isEmpty(userId)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        return userService.getUserSign(userId);
    }

    /**
     * 删除用户接口
     *
     * @param userId 用户id
     * @return Return
     */
    @PostMapping("/deleteUser")
    public Return deleteUser(String userId) {
        if (StringUtils.isEmpty(userId)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        return userService.deleteUser(userId);
    }

    /**
     * 获取宗教类别及民间信仰点个数接口
     *
     * @return Return
     */
    @GetMapping("/getCount")
    public Return getCount() {
        return userService.getCount();
    }

    /**
     * 根据用户id获取用户信息接口
     *
     * @param userId 用户id
     * @return Return
     */
    @GetMapping("/getUserInfo")
    public Return getUserInfo(String userId) {
        if (StringUtils.isEmpty(userId)) {
            return new Return(Return.FAIL, "参数缺失");
        }
        return userService.getUserInfo(userId);
    }

    /**
     * 重置密码
     *
     * @param userId 用户id
     * @return Return
     */
    @GetMapping("/resetPassword")
    public Return resetPassword(String userId) {
        if (StringUtils.isEmpty(userId)) {
            return new Return(Return.FAIL, "参数缺失");
        }
        userService.resetPassword(userId);
        return new Return(Return.SUCCESS, "重置成功");
    }
}
